package com.jmu.content.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jmu.constant.MessageConstant;
import com.jmu.content.dto.ChargeStationPageDTO;
import com.jmu.content.pojo.ChargeStation;
import com.jmu.content.result.PageResult;
import com.jmu.content.result.Result;
import com.jmu.content.service.ChargeStationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminChargeStationController")
@RequestMapping("/admin/charge-station")
public class ChargeStationController {

    @Autowired
    ChargeStationService chargeStationService;

    @GetMapping
    @ApiOperation("分页查找充电站")
    public Result<PageResult> page(ChargeStationPageDTO chargeStationPageDTO){
        PageResult pageResult = chargeStationService.pageQuery(chargeStationPageDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过id查找充电站")
    public Result<ChargeStation> getById(@PathVariable Long id){
        ChargeStation station = chargeStationService.getById(id);
        if (station==null){
            return Result.error(MessageConstant.CHARGE_STATION_NOT_FOUND);
        }
        return Result.success(station);
    }

    @PostMapping
    @CacheEvict(cacheNames = "stationCache",key = "0")
    @ApiOperation("添加充电站")
    public Result add(@RequestBody ChargeStation chargeStation){
        chargeStationService.save(chargeStation);
        return Result.success();
    }


    @PutMapping
    @CacheEvict(cacheNames = "stationCache",key = "0")
    @ApiOperation("修改充电站")
    public Result update(@RequestBody ChargeStation chargeStation){
        String name = chargeStation.getName();
        Integer status = chargeStation.getStatus();
        String location = chargeStation.getLocation();
        String image = chargeStation.getImage();
        Long pricelId = chargeStation.getPricelId();
        Integer revenue = chargeStation.getRevenue();
        LambdaUpdateWrapper<ChargeStation> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(ChargeStation::getId,chargeStation.getId())
                .set(status !=null,ChargeStation::getStatus, status)
                .set(name !=null&& name.length()!=0,ChargeStation::getName, name)
                .set(location !=null&& location.length()!=0,ChargeStation::getLocation, location)
                .set(image!=null&&image.length()!=0,ChargeStation::getImage,image)
                .set(pricelId!=null,ChargeStation::getPricelId,pricelId)
                .set(revenue!=null,ChargeStation::getRevenue,revenue);

        chargeStationService.update(updateWrapper);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "stationCache",key = "0")
    @ApiOperation("删除充电站")
    public Result delete(@PathVariable Long id){
        boolean bool = chargeStationService.removeById(id);
        if (!bool){
            return Result.error(MessageConstant.CHARGE_STATION_NOT_FOUND);
        }

        return Result.success();
    }
}

